package com.area.cth.controller.admin;

import com.area.cth.common.PageBean;
import com.area.cth.common.Result;
import com.area.cth.mapper.UserMapper;
import com.area.cth.pojo.User;
import com.area.cth.service.UserService;
import com.area.cth.utils.JwtUtil;
import com.area.cth.utils.Md5Util;
import com.area.cth.utils.ThreadLocalUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.constraints.Pattern;
import org.hibernate.validator.constraints.URL;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Tag(name = "管理端用户相关接口")
@RestController("adminUserController")
@RequestMapping("/admin/user")
@Validated
@CrossOrigin
public class UserController {
    @Autowired
    private UserService us;
    @Autowired
    private StringRedisTemplate srt;
    @Autowired
    private UserMapper userMapper;

    @Operation(summary = "查询个人信息")
    @GetMapping("/userInfo")
    public Result<User> getUserInfo() {
        Map<String, Object> map = ThreadLocalUtil.get();
        String username = (String) map.get("username");
        User user = us.findByUsername(username);
        return Result.success(user);
    }

    @Operation(summary = "分页查询")
    @GetMapping("/userList")
    public Result<PageBean<User>> getUserList(Integer pageNo, Integer pageSize, @RequestParam(required = false) String keyword) {
        PageBean<User> pb = us.getUsers(pageNo, pageSize, keyword);
        return Result.success(pb);
    }

    @Operation(summary = "根据id查询用户")
    @GetMapping("/getUserInfoById")
    public Result<User> getUserInfoById(Integer id) {
        User user = us.getUserInfoById(id);
        return Result.success(user);
    }

    @Operation(summary = "删除用户")
    @DeleteMapping
    public Result delete(Integer id) {
        userMapper.deleteById(id);
        return Result.success();
    }
}
